package com.egao.oa.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.oa.biz.PhoneBookBiz;
import com.egao.oa.entity.PhoneBook;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-08-19 10:53:21
 * @version ：1.0
 */
@Repository
public class PhoneBookBizImpl extends BaseDao implements PhoneBookBiz {

	@Override
    public JsonResult list(PageResult2<T> page, PhoneBook phoneBook, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<PhoneBook> rm = new BeanPropertyRowMapper<PhoneBook>(PhoneBook.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",p.phone,p.birthday,p.position,p.state");
		sql.append(",p.remark,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_phone_book p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(phoneBook)){
			if(Utils.isNotEmpty(phoneBook.getName())){
				sql.append(" and instr(p.name,:name)>0 ");
				map.put("name", phoneBook.getName());
			}
            if(Utils.isNotEmpty(phoneBook.getSort())&&Utils.isNotEmpty(phoneBook.getOrder())){
                sql.append(" order by p."+phoneBook.getSort()+" "+phoneBook.getOrder());
            } else {
                sql.append(" order by p.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<PhoneBook> list(PhoneBook phoneBook, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<PhoneBook> rm = new BeanPropertyRowMapper<PhoneBook>(PhoneBook.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",p.phone,p.birthday,p.position,p.state");
		sql.append(",p.remark,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_phone_book p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(phoneBook)){
			if(Utils.isNotEmpty(phoneBook.getName())){
				sql.append(" and instr(p.name,:name)>0 ");
				map.put("name", phoneBook.getName());
			}
			if(Utils.isNotEmpty(phoneBook.getPosition())){
				sql.append(" and instr(p.position,:position)>0 ");
				map.put("position", phoneBook.getPosition());
			}
			if(Utils.isNotEmpty(phoneBook.getAutocompletevalue())) {
                if (phoneBook.getAutocompletevalue().equals("it")) {
                    sql.append(" and (instr(p.position, '程序员')>0 or instr(p.position, '美工')>0 )");
                }
            }
            if(Utils.isNotEmpty(phoneBook.getSort())&&Utils.isNotEmpty(phoneBook.getOrder())){
                sql.append(" order by p."+phoneBook.getSort()+" "+phoneBook.getOrder());
            } else {
                sql.append(" order by p.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public PhoneBook getById(PhoneBook phoneBook) {
        RowMapper<PhoneBook> rm = new BeanPropertyRowMapper<PhoneBook>(PhoneBook.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",p.phone,p.birthday,p.position,p.state");
		sql.append(",p.remark,p.create_time,p.create_user,p.update_time");
		sql.append(",p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_phone_book p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 and p.id=:id ");
        map.put("id", phoneBook.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(PhoneBook phoneBook, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into oa_phone_book (");
        sql.append("organization_id,name,phone,birthday");
		sql.append(",position,state,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:phone,:birthday");
		sql.append(",:position,:state,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", phoneBook.getName());
        map.put("phone", phoneBook.getPhone());
        map.put("birthday", phoneBook.getBirthday());
        map.put("position", phoneBook.getPosition());
        map.put("state", phoneBook.getState());
        map.put("remark", phoneBook.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(PhoneBook phoneBook, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update oa_phone_book set ");
        sql.append("name=:name,phone=:phone,birthday=:birthday ");
		sql.append(",position=:position,state=:state,remark=:remark ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("name", phoneBook.getName());
        map.put("phone", phoneBook.getPhone());
        map.put("birthday", phoneBook.getBirthday());
        map.put("position", phoneBook.getPosition());
        map.put("state", phoneBook.getState());
        map.put("remark", phoneBook.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", phoneBook.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(PhoneBook phoneBook, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(phoneBook.getId().length()>0){
            for(String ID : phoneBook.getId().split(",")){
                map  = new HashMap<String, String>();
                if(ID==null || ID.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update oa_phone_book set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}